# coding: utf-8
'''
Created on 30 avr. 2011

@author: alinehuf

Tests of the UiMainWindow class
'''

import unittest
from ui.UiMainWindow import UiMainWindow
from ui.UiDimension import UiDimension #
uiD = UiDimension()

from stub.UiGameStatus import GameStatus, Damier
import sys
from PyQt4 import QtGui

class Test(unittest.TestCase):
    def setUp(self):
        self.app = QtGui.QApplication(sys.argv)
        self.mainWindow = UiMainWindow(None)
                
    def tearDown(self):
        del self.mainWindow

    def testInit(self): 
        self.assertEqual(self.mainWindow.tailleCase, 0)
        self.assertEqual(self.mainWindow.controleur, None)
        self.assertEqual(self.mainWindow.personnages, {})
        
    def testGetSize(self):
        gameStatus = GameStatus()
        gameStatus.damier = Damier()
        fenetreW = 1000
        fenetreH = 650
        uiTourHeight = 160
        uiTourWidth = 200
        UiZonesHeight = 220
        
        sizes = self.mainWindow._getSizes(gameStatus)
        
        self.assertEqual(sizes["width"], 10)
        self.assertEqual(sizes["height"], 8)
        
        # (fenetreH - UiZonesHeight) / height = 64,75
        self.assertEqual(sizes["tailleCase"], 53) 
        
        self.assertEqual(sizes["uiDamier"]['w'], 530) # 64 * height
        self.assertEqual(sizes["uiDamier"]['h'], 424)
        self.assertEqual(sizes["uiDamier"]['x'], 135)
        self.assertEqual(sizes["uiDamier"]['y'], 3)

        self.assertEqual(sizes["uiTour"]['w'], uiTourWidth)
        self.assertEqual(sizes["uiTour"]['h'], uiTourHeight)
        self.assertEqual(sizes["uiTour"]['x'], fenetreW - uiTourWidth)
        self.assertEqual(sizes["uiTour"]['y'], 0)
        
        self.assertEqual(sizes["uiActivite"]['w'], uiTourWidth)
        self.assertEqual(sizes["uiActivite"]['h'], \
                                       fenetreH - uiTourHeight - UiZonesHeight)
        self.assertEqual(sizes["uiActivite"]['x'], fenetreW - uiTourWidth)
        self.assertEqual(sizes["uiActivite"]['y'], uiTourHeight)
        
        self.assertEqual(sizes["uiPerso1"]['w'], fenetreW / 4)
        self.assertEqual(sizes["uiPerso1"]['h'], UiZonesHeight)
        self.assertEqual(sizes["uiPerso1"]['x'], 0)
        self.assertEqual(sizes["uiPerso1"]['y'], fenetreH - UiZonesHeight)

        self.assertEqual(sizes["uiPerso2"]['w'], fenetreW / 4)
        self.assertEqual(sizes["uiPerso2"]['h'], UiZonesHeight)
        self.assertEqual(sizes["uiPerso2"]['x'], fenetreW / 4)
        self.assertEqual(sizes["uiPerso2"]['y'], fenetreH - UiZonesHeight)
        
        self.assertEqual(sizes["uiAction"]['w'], fenetreW / 4)
        self.assertEqual(sizes["uiAction"]['h'], UiZonesHeight)
        self.assertEqual(sizes["uiAction"]['x'], fenetreW / 2)
        self.assertEqual(sizes["uiAction"]['y'], fenetreH - UiZonesHeight)
        
        self.assertEqual(sizes["uiTerrain"]['w'], fenetreW / 4)
        self.assertEqual(sizes["uiTerrain"]['h'], UiZonesHeight)
        self.assertEqual(sizes["uiTerrain"]['x'], fenetreW / 4 * 3)
        self.assertEqual(sizes["uiTerrain"]['y'], fenetreH - UiZonesHeight)
        
    def testAfficheNouvellePartie(self):
        gameStatus = GameStatus()
        gameStatus.damier = Damier()
        self.mainWindow.afficheNouvellePartie(gameStatus)
        self.assertEqual(self.mainWindow.tailleCase, 53)
        self.assertEqual(self.mainWindow.uiPerso1.titre.text(), \
                                                    u"Personnage sélectionné")
        self.assertEqual(self.mainWindow.uiPerso2.titre.text(), \
                                                    u"Personnage cible")
        self.assertEqual(self.mainWindow.uiAction.titre.text(), u"Action")
        self.assertEqual(self.mainWindow.uiTerrain.titre.text(), u"Terrain")
        self.assertEqual(self.mainWindow.uiActivite.titre.text(), u"Activité")
        self.assertEqual(self.mainWindow.uiTour.titre.text(), u"Tour")
    
    def testUpdateUiZone(self):
        gameStatus = GameStatus()
        gameStatus.damier = Damier()
        self.mainWindow.afficheNouvellePartie(gameStatus)
        self.assertEqual(self.mainWindow.uiTour.texte.text(), u"")
        self.mainWindow.updateUiZone("texte de test", "uiTour")
        self.assertEqual(self.mainWindow.uiTour.texte.text(), u"texte de test")

    def testUpdateUiAction(self):
        gameStatus = GameStatus()
        gameStatus.damier = Damier()
        self.mainWindow.afficheNouvellePartie(gameStatus)
        self.assertEqual(self.mainWindow.uiAction.actions, [])
        self.mainWindow.updateUiZone(["move", "attack"], "uiAction")
        self.assertEqual(self.mainWindow.uiAction.actions[0].text(), "move")
        self.assertEqual(self.mainWindow.uiAction.actions[1].text(), "attack")

    def testUpdateUiPersonnage(self):
        gameStatus = GameStatus()
        gameStatus.damier = Damier()
        self.mainWindow.afficheNouvellePartie(gameStatus)
        tailleCase = 53
        offsetX = uiD.imagesPersonagesOffsetX * tailleCase
        offsetY = uiD.imagesPersonagesOffsetY * tailleCase
        self.mainWindow.creePersonnage("Asura")
        for x in range(0, 10):
            for y in range(0, 8):
                nomCase = chr(65 + x) + str(y+1) # A1, A2... B5...
                caseX = self.mainWindow.uiDamier.cases[nomCase].x * \
                                                        tailleCase + offsetX
                caseY = self.mainWindow.uiDamier.cases[nomCase].y * \
                                                        tailleCase + offsetY
                self.mainWindow.updatePersonnage("Asura", nomCase)
                self.assertEqual(self.mainWindow.personnages["Asura"].x, caseX)
                self.assertEqual(self.mainWindow.personnages["Asura"].y, caseY)

if __name__ == "__main__":
    unittest.main()